package com.ftg.wymall.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ftg.wymall.comm.tool.TokenUtil;
import com.ftg.wymall.comm.vo.CodeStatusEnum;
import com.ftg.wymall.comm.vo.ConstUtil;
import com.ftg.wymall.comm.vo.RespMessUtil;
import com.ftg.wymall.service.UserCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户认证中心的方法
 *   包含管理员登陆方法
 *
 *   会员登陆方法
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/10/19 上午9:28
 */
@RestController
@RequestMapping("/usercenter")
@Api(tags = "认证中心")
public class UserCenterController {


    @Resource
    UserCenterService userCenterService;

    @PostMapping("/admin/login")
    @ApiOperation("管理员后台登陆功能")
    public RespMessUtil<String> adminLogin(
            @ApiParam(name = "username",value = "用户名",example = "admin") @RequestParam(value = "username") String username,
            @ApiParam(name = "password",value = "密码",example = "123") @RequestParam(value = "password") String password
    ){
        return userCenterService.accessUmlAdminLogin(username,password);
    }


    @PostMapping("/admin/getUserInfo")
    @ApiOperation("后台登陆成功以后获取登陆用户信息方法")
    public RespMessUtil<JSONObject> getUserInfo(
            @ApiParam(name = ConstUtil.TOKEN_NAME,value = ConstUtil.TOKEN_NAME_ZH,required = true) @RequestHeader(value = ConstUtil.TOKEN_NAME) String token
    ){
        System.out.println(token);
        JSONObject userinfo = null;
        try{
            userinfo = TokenUtil.parseTokenWithObject(token);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS,userinfo);
        }catch (Exception e){
            return RespMessUtil.build(CodeStatusEnum.INVALID_TOKEN);
        }
    }

    @GetMapping("/admin/demo")
    @ApiOperation("测试 方法")
    public RespMessUtil<String> logout(
    ){
        return RespMessUtil.build(CodeStatusEnum.SUCCESS,"我是10001里面的方法");
    }
}
